﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RestSharp;
using Convo.POCOs;

namespace Convo
{
    public class ConvoClient : RestClient
    {
        public ConvoClient() : base() { }
        public ConvoClient(string url) : base(url) { }

        public RestRequest loginRequest(string email, string password)
        {
            var request = new RestRequest("LoginUser", Method.POST);
            request.RequestFormat = DataFormat.Json;

            request.AddBody(new LoginRequest{ email = email, password = password });

            return request;
        }

        public RestRequest registrationRequest(string name, string email, string password)
        {
            var request = new RestRequest("RegisterUser", Method.PUT);
            request.RequestFormat = DataFormat.Json;

            request.AddBody(new RegistrationRequest { name = name, email = email, password = password });

            return request;
        }

        public RestRequest friendsRequest(int userId)
        {
            var request = new RestRequest("GetFriends", Method.GET);

            request.AddParameter("userId", userId);
            return request;
        }

        public RestRequest setupChatRequest(string userId)
        {
            var request = new RestRequest("GetChat", Method.GET);

            request.AddParameter("userId", userId);
            return request;
        }

        public override RestRequestAsyncHandle ExecuteAsync<T>(IRestRequest request, Action<RestResponse<T>, RestRequestAsyncHandle> callback)
        {
            return base.ExecuteAsync<T>(request, callback);
        }
    }

    public static class ConvoClientExtensions
    {
        public static RestRequestAsyncHandle ExecuteAsync<T>(this IRestClient client, IRestRequest request, Action<RestResponse<T>> callback) where T : new()
        {
            return client.ExecuteAsync<T>(request, (response, asyncHandle) => callback(response));
        }
    }
}
